package com.hd.pzhwlxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hd.pzhwlxt.mapper.MaterialMapper;
import com.hd.pzhwlxt.mapper.MaterialpriceMapper;
import com.hd.pzhwlxt.pojo.Material;
import com.hd.pzhwlxt.pojo.Materialprice;
import com.hd.pzhwlxt.service.MaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @create: 2022-09-21 9:01
 * @author: 怀念天空
 * @Description:
 */

@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements MaterialService {

    @Autowired
    private MaterialMapper materialMapper;

    @Autowired
    private MaterialpriceMapper materialpriceMapper;

    /**
     * 获取材料基本信息及价格
     *
     * @return
     */
    @Override
    public List<Material> getAllMaterialInfo() {
        return materialMapper.getAllMateriaAndPrice();
    }

    @Override
    public Page<Material> getAllMateriaAndPricePage(Integer pageNum, Integer pageSize) {
        return materialMapper.getAllMateriaAndPricePage(new Page<>(pageNum, pageSize));
    }

    @Override
    public boolean deleteByIds(List<Integer> asList) {
        for (Integer i : asList) {
            materialMapper.deleteById(i);
            QueryWrapper<Materialprice> wrapper = new QueryWrapper<>();
            wrapper.select("id").eq("material_id", i);
            materialpriceMapper.delete(wrapper);
        }
        return true;
    }

    @Override
    public boolean updatematerial(Material material) {
        materialMapper.updateById(material);
        Materialprice materialprice = material.getMaterialprice();
        materialprice.setMaterialId(material.getId());
        QueryWrapper<Materialprice> wrapper = new QueryWrapper<>();
        wrapper.eq("material_id", material.getId());
        return materialpriceMapper.update(materialprice, wrapper) > 0;
    }

    @Override
    public Material getMaterialById(Integer id) {
        return materialMapper.getMaterialAndPriceById(id);
    }

    @Override
    public boolean addMaterial(Material material) {
        materialMapper.insert(material);
        Materialprice materialprice = material.getMaterialprice();
        materialprice.setMaterialId(material.getId());
        return materialpriceMapper.insert(materialprice) > 0;
    }
}
